﻿using System;
using System.Web.UI.WebControls;
using Alserve.Ffl.Data;

public partial class ffl_anonymous_results : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetMonths();
            MonthDropDown.SelectedValue = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).ToShortDateString();
            GetCompetitions();
            RefreshView();
        }
    }

    private void GetCompetitions()
    {
        Competition thisCompetition = new Competition();
        if (thisCompetition.LoadAll())
        {
            CompetitionDropDown.DataSource = thisCompetition.DefaultView;
            CompetitionDropDown.DataValueField = Competition.ColumnNames.Id;
            CompetitionDropDown.DataTextField = Competition.ColumnNames.CompetitionName;
            CompetitionDropDown.DataBind();
        }
    }

    private void GetMonths()
    {
        //Get previous August
        DateTime startDate = DateTime.Today;

        if (startDate.Month == 7)
            startDate = startDate.AddMonths(1);
        else
        {
            while (startDate.Month != 8)
                startDate = startDate.AddMonths(-1);
        }

        for (Int32 monthCount = 1; monthCount <= 10; monthCount++)
        {
            ListItem newItem = new ListItem(startDate.ToString("MMMM") + " " + startDate.Year.ToString(), new DateTime(startDate.Year, startDate.Month, 1).ToShortDateString());
            MonthDropDown.Items.Add(newItem);
            startDate = startDate.AddMonths(1);
        }

    }

    private void GetCompetitionDates(Int32 competitionId, DateTime monthDate)
    {
        Match thisMatch = new Match();
        thisMatch.Where.CompetitionId.Value = Convert.ToInt32(CompetitionDropDown.SelectedValue);
        thisMatch.Where.MatchDate.Operator = MyGeneration.dOOdads.WhereParameter.Operand.Between;
        thisMatch.Where.MatchDate.BetweenBeginValue = monthDate.ToOADate();
        thisMatch.Where.MatchDate.BetweenEndValue = monthDate.AddMonths(1).AddMilliseconds(-1).ToOADate();
        
        thisMatch.Query.Distinct = true;
        thisMatch.Query.AddOrderBy(Match.ColumnNames.MatchDate, MyGeneration.dOOdads.WhereParameter.Dir.DESC);
        thisMatch.Query.AddResultColumn(Match.ColumnNames.MatchDate);

        DateRepeater.DataSource = thisMatch.Query.Load() ? thisMatch.DefaultView : null;
        DateRepeater.DataBind();
    }

    protected void CompetitionDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        RefreshView();
    }
    protected void MonthDropDown_DelectedIndexChanged(object sender, EventArgs e)
    {
        RefreshView();
    }

    private void RefreshView()
    {
        GetCompetitionDates(Convert.ToInt32(CompetitionDropDown.SelectedValue),Convert.ToDateTime(MonthDropDown.SelectedValue));
    }

    protected void DateRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HiddenField matchDateHidden = (HiddenField)e.Item.FindControl("MatchDateHidden");
            DateTime matchDate = Convert.ToDateTime(matchDateHidden.Value);
            Repeater matchDateRepeater = (Repeater)e.Item.FindControl("MatchRepeater");

            _ResultsView thisResult = new _ResultsView();
            thisResult.Where.CompetitionId.Value = Convert.ToInt32(CompetitionDropDown.SelectedValue);
            thisResult.Where.MatchDate.Value = matchDate;

            matchDateRepeater.DataSource = thisResult.Query.Load() ? thisResult.DefaultView : null;
            matchDateRepeater.DataBind();

        }
    }
}
